package com.gopay.webtest.controller.cbrmb;

import java.util.Date;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.time.StopWatch;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.gopay.webtest.controller.base.BaseController;
import com.gopay.webtest.util.BaseSign;
import com.gopay.webtest.util.DateUtils;
import com.gopay.webtest.vo.PayInfo;
import com.gopay.webtest.xml.XmlDao;

/**
 * 网关跨境支付(进口)Controller
 * 
 * AuthController.java
 * @author qianld
 * @2015-11-09  www.gopay.com.cn Inc.All rights reserved.
 */
@RequestMapping("/cbrmb")
@Controller
public class CbrmbInController extends BaseController {

    @Resource(name = "xmlDao")
    private XmlDao xmlDao;

    @Resource(name = "paySignHandler")
    private BaseSign paySignHandler;

    /**
     * 跳转到申请页面
     * 
     * @return
     */
    @RequestMapping("/cbrmbpay")
    public ModelAndView apply() {
        ModelAndView mv = new ModelAndView();

        try {
        	//读applicationContext.xml中的信息
            Map<String, String> map = xmlDao.readData4Map("cbrmbpay", "payapply");

            if (map.containsKey("gopayServerTime")) {
                map.remove("gopayServerTime");
                map.put("gopayServerTime", DateUtils.format(new Date(), "yyyyMMddHHmmss"));
            }
            if (map.containsKey("tranDateTime")) {
                map.remove("tranDateTime");
                map.put("tranDateTime", DateUtils.format(new Date(), "yyyyMMddHHmmss"));
            }

            mv = new ModelAndView("cbrmb.apply");
            mv.addObject("payInfo", map);
        } catch (Exception e) {
            logger.error("跨境支付页面出问题了", e);
        }

        return mv;
    }

    /**
     * 提交订单到国付宝
     * 
     * @param payInfo
     * @return
     */
    @RequestMapping("/paySubmit")
    public ModelAndView paySubmit(PayInfo payInfo) {
        ModelAndView mav = new ModelAndView();
        String backgroundMerUrl = payInfo.getBackgroundMerUrl();
        if (backgroundMerUrl != null && !backgroundMerUrl.equals("")) {
            backgroundMerUrl = backgroundMerUrl + "?id=" + payInfo.getVerficationCode();
        }

        String frontMerUrl = payInfo.getFrontMerUrl();
        if (frontMerUrl != null && !frontMerUrl.equals("")) {
            frontMerUrl = frontMerUrl + "?id=" + payInfo.getVerficationCode();
        }
        payInfo.setBackgroundMerUrl(backgroundMerUrl);
        payInfo.setFrontMerUrl(frontMerUrl);
        
        StopWatch sw  = new StopWatch();
        
        // 设置MD5加密字段
        try {
            sw.start();
            paySignHandler.sign(payInfo.getTranCode() + "_" + payInfo.getVersion(), "signType", "signValue", payInfo, getRequest());
            sw.stop();
            logger.info("step1 time:"+sw.getTime());
            logger.info("后台notify密文值:" + payInfo.getSignValue());
        } catch (Exception e) {
            e.printStackTrace();
        }
        String signedValue = payInfo.getSignValue();
//        /* 是否是wap支付标志位 */
//        String isWapPay = payInfo.getIsWapPay();
        if (null == signedValue) {
            // 加密操作失败,置返回码为身份认证md5加密失败
            payInfo.setRespCode("MR08");
            mav.setViewName("cbrmb.apply");
        }else {
            mav.setViewName("gopay.webtest.cbrmbPayFormSubmit");
        }
        if (!payInfo.getCharset().equals("2")) {
            mav.setViewName("gopay.webtest.cbrmbPayFormSubmit_gbk");
        } else {
            mav.setViewName("gopay.webtest.cbrmbPayFormSubmit");
        }
        mav.addObject("payInfo", payInfo);
        return mav;
    }
}
